/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.isip.edgeDetection;

import com.azon.ip.utils.Matrix3x3;
import com.isip.transformation.difference.MatrixTransformation;

/**
 *
 * @author Ahmad Fauzan
 */
public class Robert2EdgeDetection implements EdgeDetection{

    @Override
    public int[][] detect(int[][] matrixImage) {
        
        float[] matrixConv1 = {1,0,0,
                                0,-1,0,
                                0,0,0};
        float[] matrixConv2 = {0,1,0,
                                -1,0,0,
                                0,0,0};
        int[][] tempImage1,tempImage2;
        MatrixTransformation t1 = new MatrixTransformation();
        t1.setMatrixTransformation(matrixConv1);
        tempImage1 = t1.doTransform(matrixImage);
        t1.setMatrixTransformation(matrixConv2);
        tempImage2 = t1.doTransform(matrixImage);
        
        
        float[] matrixConv3 = {0,-1,0,
                                1,0,0,
                                0,0,0};
        float[] matrixConv4 = {-1,0,0,
                                0,1,0,
                                0,0,0};
        int[][] tempImage3,tempImage4;
        t1.setMatrixTransformation(matrixConv3);
        tempImage3 = t1.doTransform(matrixImage);
        t1.setMatrixTransformation(matrixConv4);
        tempImage4 = t1.doTransform(matrixImage);
        
        return sum(sum(tempImage3,tempImage4),sum(tempImage1, tempImage2));
    }
    
    private int[][] sum(int[][] matrix1, int[][] matrix2) {
        int[][] temp = new int[matrix1.length][matrix1[0].length];
        for(int i=0; i < matrix1.length; i ++) {
            for(int j=0; j < matrix1[i].length;j++) {
                temp[i][j] = (int)Math.sqrt(matrix1[i][j]*matrix1[i][j] + matrix2[i][j]*matrix2[i][j]);
            }
        }
        return temp;
    }
    
}
